package top.qform.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.qform.config.Demo3;
import top.qform.config.Demo5;
import top.qform.config.Demo6;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@RestController
public class TestController {

    @Value("${demo1.name}")
    String demo1Name;

    @Value("${demo1.address}")
    String demo1Address;

    @Value("#{'${demo1.like}'.split(',')}")
    String demo1Like1;

    @Value("${demo1.like}")
    String[] demo1Like2;

    @Autowired
    Environment environment;

    @Autowired
    Demo3 demo3;

    @Autowired
    Demo5 demo5;

    @Autowired
    Demo6 demo6;

    @GetMapping("/demo1")
    public String demo1() {
        return String.format("name：%s<br/>address：%s<br/>like1：%s<br/>like2：%s", demo1Name, demo1Address, demo1Like1, Arrays.toString(demo1Like2));
    }

    @GetMapping("/demo2")
    public String demo2() {
        String name = environment.getProperty("demo2.name");
        String address = environment.getProperty("demo2.address");
        List like = environment.getProperty("demo2.like", List.class);
        return String.format("name：%s<br/>address：%s<br/>like：%s<br/>", name, address, like);
    }

    @GetMapping("/demo3")
    public String demo3() {
        return String.format("name：%s<br/>address：%s<br/>like：%s<br/>map1:%s<br/>map2:%s", demo3.getName(), demo3.getAddress(), demo3.getLike(), demo3.getMap1(), demo3.getMap2());
    }

    @GetMapping("/demo4")
    public String demo4() throws IOException {
        Properties properties = PropertiesLoaderUtils.loadAllProperties("demo4.properties");
        return String.format("name：%s<br/>address：%s", properties.getProperty("demo4.name"), properties.getProperty("demo4.address"));
    }

    @GetMapping("/demo5")
    public String demo5() {
        return String.format("duration1：%s<br/>duration2：%s<br/>dataSize1：%s<br/>dataSize2：%s", demo5.getDuration1(), demo5.getDuration2(), demo5.getDataSize1(), demo5.getDataSize2());
    }

    @GetMapping("/demo6")
    public String demo6() {
        return String.format("weight：%s", demo6.getWeight());
    }

}
